package samples.flexstore {
	import com.metasoft.flex.MetasoftULC;
	import com.metasoft.flex.util.MetasoftHelper;
	
	import flash.net.SharedObject;
	
	import mx.collections.ArrayCollection;
	import mx.rpc.events.ResultEvent;
	

	[Bindable]
	public class ProductSearch {

		public function ProductSearch() {
	        super();
		}

		public var keyword : String;
		public var synopsis : String;
		public var title : String;

		public var credits : String;

		public var category : String;

        /**
		 * Handler for ProductView
		 */ 						
        public function productServiceResultHandler(event:ResultEvent) : void {

			if (event != null && event.result != null) {

				var so:SharedObject = SharedObject.getLocal(MetasoftULC.LoggedInUser);
				if (event.result.getDataResult == null) {
					return;
				}
				var tvaMain:Object = event.result.getDataResult.TVAMain;
	            if (tvaMain == null) {
	            	return;
	            }
	            var programDescription:Object = tvaMain.programDescription;
	            if (programDescription == null) {
	            	return;
	            }
	            var programInformationTable:Object = programDescription.programInformationTable;
	            if (programInformationTable == null) {
	            	return;
	            }
	            var programInformationArray:Array = programInformationTable.programInformationArray;
	            if (programInformationArray != null && programInformationArray.length > 0) {

					var count : int = 1;
					var j:int = 0;
		            var pi:Object = new Object();
		            var programItems:ArrayCollection = new ArrayCollection();
		            for each(pi in programInformationArray) {
		
						var  prgItem:Product = new Product();  
						var basicDescription:Object = pi.basicDescription as Object;
						if (basicDescription == null) {
							continue;
						}
						prgItem.productId = count;
		                prgItem.imagePath = MetasoftULC.ImageDir;
//						prgItem = new ProgramItem();
						if (pi.programId != null) {
							prgItem.id = String(pi.programId);
						}
						if (basicDescription.titleArray[0] != null) {

							prgItem.title = basicDescription.titleArray[0].stringValue;
//							product.name = asicDescription.titleArray[0].stringValue;
						}
						if (basicDescription.synopsisArray[0] != null) {

							prgItem.synopsis = basicDescription.synopsisArray[0].stringValue;
//							product.description = basicDescription.synopsisArray[0].stringValue;
						}
						if (basicDescription.duration != null) {
							prgItem.duration = basicDescription.duration;
						}
						if (basicDescription.thumbnail != null) {

							if (basicDescription.thumbnail.stringValue != null) {

								var thumbnail : String = MetasoftUtils.escapeSequence(basicDescription.thumbnail.stringValue);
								prgItem.thumbnailName 	= thumbnail;
								prgItem.thumbnail 		= MetasoftUtils.getImageURI(thumbnail);
								if (basicDescription.preview == null) {
									prgItem.preview = prgItem.thumbnail;
								}
							}
						}
						if (basicDescription.preview != null) {

							if (basicDescription.preview.stringValue != null) {

								var preview : String = MetasoftUtils.escapeSequence(basicDescription.preview.stringValue);
								prgItem.preview = MetasoftUtils.getImageURI(preview);
								if (basicDescription.thumbnail == null) {
									prgItem.thumbnail = prgItem.preview;
								}
							}
						}
						if (basicDescription.keywordArray != null &&
							basicDescription.keywordArray.length > 0) {
	
							prgItem.tags = new Array();
							for(j = 0; j < basicDescription.keywordArray.length; j++) {
	
								if (basicDescription.keywordArray[j] != null) {
									prgItem.tags.push(basicDescription.keywordArray[j].stringValue);
								}
							}
						}
						if (basicDescription.cost != null)	{
							prgItem.price = MetasoftUtils.getCost(basicDescription.cost.stringValue);
						} else { 
							prgItem.price = "0.0";
						}
						var thumbnailStreamArray:Array = null;
						if (basicDescription.mediaTitleArray != null && 
						 	basicDescription.mediaTitleArray.length > 0 ) {
	
							thumbnailStreamArray = new Array();
							for (j = 0; j < basicDescription.mediaTitleArray.length; j++) {
	
								if (basicDescription.mediaTitleArray[j] != null) {
							 		thumbnailStreamArray.push(basicDescription.mediaTitleArray[j].stringValue);
							 	}
						 	}
						 	prgItem.thumbnailStream = thumbnailStreamArray;
						} else if (basicDescription.thumbnail != null) {
	
							thumbnailStreamArray = new Array();
							thumbnailStreamArray.push(basicDescription.thumbnail.stringValue);
						}
						if (basicDescription.genreArray != null && 
							basicDescription.genreArray.length > 0) {
	
							var genreArray:Array 	= basicDescription.genreArray;
							prgItem.genreArray		= genreArray;
							prgItem.content    		= MetasoftHelper.getGenreFromObject(genreArray, MetasoftULC.CS_Content, prgItem);
							prgItem.intentions 		= MetasoftHelper.getGenreFromObject(genreArray, MetasoftULC.CS_Intention, prgItem);
							prgItem.atmosphere 		= MetasoftHelper.getGenreFromObject(genreArray, MetasoftULC.CS_Atmosphere, prgItem);
							prgItem.format     		= MetasoftHelper.getGenreFromObject(genreArray, MetasoftULC.CS_Format, prgItem);
							prgItem.intendedAudience= MetasoftHelper.getGenreFromObject(genreArray, MetasoftULC.CS_IntendedAudience, prgItem);
							prgItem.mediaType  		= MetasoftHelper.getGenreFromObject(genreArray, MetasoftULC.CS_MediaType, prgItem);
					    }
						var temp:Array = new Array();
	//					for each (var img:Object in prgItem.thumbnailStream) {
	////						if (img != null && img.titleImage != null)
	////							temp.push((String(img.titleImage.mediaUri)));
	//					}
	//					if (temp.length > 0) {
	//						prgItem.thumbnailStream = temp;
	//					}
	//					temp = new Array();
						if (basicDescription.relatedMaterialArray != null && 
							basicDescription.relatedMaterialArray is Array && 
							basicDescription.relatedMaterialArray.length > 0) {
							temp = basicDescription.relatedMaterialArray;
						}
						prgItem.relatedMaterial = new Array();
						var i:int = 0;
						var officialURI:String = "";
						for each (var rm:Object in temp) {
	
							var icon:String = "";
							var link:String = "";
							if (rm != null && 
								rm.promotionalMediaArray != null && 
								rm.promotionalMediaArray.titleVideo != null) {
	
								prgItem.trailers[i] = rm.promotionalMediaArray.titleVideo.mediaUri;
								icon = rm.promotionalMediaArray.titleVideo.mediaUri;
							} else {
								icon = prgItem.thumbnail;
							}
							if (rm != null && rm.mediaLocator != null && rm.mediaLocator.mediaUri != null) {

								link = rm.mediaLocator.mediaUri;
								prgItem.highlight1 = rm.mediaLocator.mediaUri;
							}
							link = MetasoftUtils.getURI(link);
							prgItem.relatedMaterial[i] = [icon,link];
							officialURI = link;
							i++;
						}
						if(officialURI) {
							prgItem.officialURI = officialURI;
						}
						if (basicDescription.creditsList != null) {
							prgItem.creditItems = MetasoftHelper.getCreditItemsFromProxy(basicDescription.creditsList);
						}
						// for parental guidance
						if (basicDescription.parentalGuidanceArray != null && 
							basicDescription.parentalGuidanceArray.length > 0) {
							prgItem.parentalGuidance = MetasoftHelper.getParentalGuidanceFromProxy(basicDescription.parentalGuidanceArray);
						}
						//for ReleaseDate
						if (basicDescription.releaseInformationArray != null && 
							basicDescription.releaseInformationArray.length > 0 &&
							basicDescription.releaseInformationArray[0].releaseDate != null) {
							prgItem.releaseDate =  basicDescription.releaseInformationArray[0].releaseDate.dayAndYear;
						}
						programItems.addItem(prgItem);
						count++;
		            }
		            catalog = programItems
    	            exchange.catalog = catalog;
		  		}
		 	}
    	}
	}
}
